package bpm.facade.document;

import bpm.domain.archive.ArchiveState;
import bpm.domain.document.Category;
import bpm.domain.document.Document;
import bpm.domain.document.DocumentConfiguration;
import bpm.domain.document.DocumentDefinition;
import bpm.domain.document.DocumentView;
import bpm.domain.document.Template;
import bpm.domain.meta.Priority;
import bpm.domain.meta.Security;
import bpm.domain.user.OrgGroup;
import bpm.domain.user.OrgUser;

/**
 * 
 * @author ZhaoWangLong Aug 15, 2011
 * 
 */
public class DocumentTransition {
	public static Document transDocumentView(DocumentView docView) {
		Document doc = new Document();
		doc.setId(docView.getId());

		DocumentDefinition docDef = new DocumentDefinition();
		docDef.setId(docView.getDocumentDefinitionId());
		Template editTemplate = new Template();
		editTemplate.setId(docView.getEditTemplateId());
		docDef.setEditTemplate(editTemplate);

		DocumentConfiguration docConfig = new DocumentConfiguration();
		docConfig.setDocumentDefinition(docDef);
		docConfig.setProcessInstanceId(docView.getProcessId());
		docConfig.setLock(docView.getDocumentLock());
		docConfig.setStringCompleted(docView.getCompleted());
		if(docView.getArchiveState()!=null&&!"".equals(docView.getArchiveState()))
			docConfig.setArchiveState(ArchiveState.valueOf(docView.getArchiveState()));
		docConfig.setArchiveTime(docView.getArchiveTime());
		doc.setDocumentConfiguration(docConfig);

		Category docCategory = new Category();
		docCategory.setId(docView.getDocumentCategory());
		doc.setCategory(docCategory);

		doc.setTitle(docView.getTitle());
		doc.setBgtType(docView.getBgtType());
		if("".equals(docView.getBgtCode())){
			doc.setBgtCode(null);
		}else{
			doc.setBgtCode(docView.getBgtCode());
		}
		Priority priority = new Priority();
		priority.setId(docView.getPriorityId());
		priority.setName(docView.getPriorityName());
		doc.setPriority(priority);

		Security security = new Security();
		security.setId(docView.getSecurityLevelId());
		security.setName(docView.getSecurityLevelName());
		doc.setSecurity(security);

		OrgUser drafter = new OrgUser();
		drafter.setId(docView.getDrafter());
		drafter.setName(docView.getDrafterName());
		OrgGroup pGroup = new OrgGroup();
		pGroup.setId(docView.getDraftDeptId());
		pGroup.setName(docView.getDraftDeptName());
		drafter.setOrgGroup(pGroup);
		doc.setDrafter(drafter);

		doc.setDraftTime(docView.getDraftTime());
		doc.setOfficialCode(docView.getOfficialCode());
		doc.setDraftDept(docView.getDraftDept());
		doc.setFromCode(docView.getFromCode());
		doc.setFromOrg(docView.getFromOrg());
		doc.setKeyword(docView.getKeyword());
		doc.setDeadline(docView.getDeadline());
		doc.setNews(docView.isNews());
		doc.setRule(docView.isRule());
		doc.setOpen(docView.isOpen());
		doc.setEntries(docView.getDocumentEntries());
		doc.setNotes(docView.getDocumentNotes());
		doc.setAttachments(docView.getAttachments());
		return doc;
	}

	public static DocumentView transDocument(Document doc) {
		if (doc != null) {
			DocumentView docView = new DocumentView();
			docView.setId(doc.getId());
			
			if(doc.getDocumentConfiguration() != null){
				if(doc.getDocumentConfiguration().getDocumentDefinition()!=null){
					Integer docDefId =doc.getDocumentConfiguration().getDocumentDefinition().getId();
					docView.setDocumentDefinitionId(docDefId);
					if(doc.getDocumentConfiguration()	.getDocumentDefinition()
												.getEditTemplate()!=null){
						Integer editTemplateId = doc.getDocumentConfiguration()	.getDocumentDefinition()
						.getEditTemplate().getId();
						docView.setEditTemplateId(editTemplateId);
					}
				}
				String processId =doc.getDocumentConfiguration().getProcessInstanceId();
				docView.setProcessId(editNull(processId));
				docView.setDocumentLock( editNull(doc.getDocumentConfiguration().getLock()));
				docView.setCompleted( doc.getDocumentConfiguration().getCompletedString());
				if(doc.getDocumentConfiguration().getArchiveState()!=null){
					docView.setArchiveState(doc.getDocumentConfiguration().getArchiveState().name());
				}
				docView.setArchiveTime(doc.getDocumentConfiguration().getArchiveTime());
			}else{
				docView.setProcessId("");
				docView.setDocumentLock( "");
			}
			
			
		
			if(doc.getCategory() != null){
				docView.setDocumentCategory(doc.getCategory().getId());
				docView.setDocumentCategoryName(doc.getCategory().getName());
			}else{
				docView.setDocumentCategory(null);
				docView.setDocumentCategoryName("");
			}
			
			
			docView.setTitle(editNull(doc.getTitle()));
			docView.setBgtType(editNull(doc.getBgtType()));
			docView.setBgtCode(editNull(doc.getBgtCode()));
			if(doc.getPriority() != null ){
				docView.setPriorityId( doc.getPriority().getId());
				docView.setPriorityName( doc.getPriority().getName());
			}else{
				docView.setPriorityId("");
				docView.setPriorityName( "");
			}
			if(doc.getSecurity() != null ){
				docView.setSecurityLevelId(doc.getSecurity().getId());
				docView.setSecurityLevelName(doc.getSecurity().getName());
			}else{
				docView.setSecurityLevelId("");
				docView.setSecurityLevelName("");
			}
			
			
			if(doc.getDrafter()!=null){
				docView.setDrafter(editNull(doc.getDrafter().getId()));
				docView.setDrafterName(editNull(doc.getDrafter().getName()));
				if(doc.getDrafter().getOrgGroup()!=null){
					docView.setDraftDeptId( editNull(doc.getDrafter().getOrgGroup().getId()));
					docView.setDraftDeptName(editNull(doc.getDrafter().getOrgGroup().getName()));
				}else{
					docView.setDraftDeptId("");
					docView.setDraftDeptName("");
				}
			}else{
				docView.setDrafter("");
				docView.setDrafterName("");
				docView.setDraftDeptId("");
				docView.setDraftDeptName("");
			}
			docView.setDraftTime(doc.getDraftTime());
			docView.setOfficialCode(editNull(doc.getOfficialCode()));
			docView.setDraftDept(editNull(doc.getDraftDept()));
			docView.setFromCode(editNull(doc.getFromCode()));
			docView.setFromOrg(editNull(doc.getFromOrg()));
			docView.setKeyword(editNull(doc.getKeyword()));
			docView.setDeadline(editNull(doc.getDeadline()));
			docView.setNews(doc.isNews());
			docView.setRule(doc.isRule());
			docView.setOpen(doc.isOpen());
			docView.setDocumentEntries(doc.getEntries());
			docView.setDocumentNotes(doc.getNotes());
			docView.setAttachments(doc.getAttachments());
			return docView;
		} else {
			return null;
		}

	}
	private static String  editNull(String str){
		if(str==null){
			return "";
		}
		return str;
	}
}
